package com.ossjk.qlh.system.service.impl;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.UUID;

import org.springframework.stereotype.Service;

import com.ossjk.core.system.api.IUploadCommonApi;

/**
 * 
 * Copyright © 2021 QLH. Tech Ltd. All rights reserved.
 * 
 * @Package: com.ossjk.qlh.system.service.impl
 * @ClassName: DictionaryCommonApiImpl
 * @Description: TODO
 * @author: chair
 * @date: 2021年3月3日 下午10:08:31
 */
@Service
public class UploadCommonApiImpl implements IUploadCommonApi {

	@Override
	public String upload(InputStream inputStream, String fileName, String rootPath, String path, String uri)
			throws IOException {
		String suffixName = fileName.substring(fileName.lastIndexOf(".")); // 后缀名
		String newFileName = UUID.randomUUID() + suffixName; // 新文件名
		File dest = new File(rootPath + File.separator + path + File.separator + newFileName);
		if (!dest.getParentFile().exists()) {
			dest.getParentFile().mkdirs();
		}
		Files.copy(inputStream, Paths.get(rootPath + File.separator + path + File.separator + newFileName));
		String url = File.separator + uri + File.separator + path + File.separator + newFileName;
		return url;
	}

}
